package website.lucaswork.survey.api.converters;

import javax.persistence.NoResultException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import website.lucaswork.survey.core.entities.Survey;
import website.lucaswork.survey.core.services.SurveyService;

@Component
public class SlugToSurveyConverter implements Converter<String, Survey> {

	@Autowired
	SurveyService surveyService;
	
	@Override
	public Survey convert(String slug) {
		Survey survey = surveyService.getSruveyBySlug(slug);
		if (survey == null) {
			throw new NoResultException("No survey found for this slug: " + slug);
		}
		return survey;
	}

}
